package ui.troopmanager.mooresr.us;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import troopmanager.mooresr.us.DataManager;
import troopmanager.mooresr.us.Vehicle;



public class VehicleDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JButton addButton;
	private JButton editButton;
	private JButton deleteButton;
	private JButton okButton;
	private JList vehicleList;
	private JScrollPane jScrollPane0;
	
	private VehicleDetailDialog vehicleDetailDialog = new VehicleDetailDialog();
	
	public VehicleDialog() {
		initComponents();
	}



	private void initComponents() {
		BorderLayout layout = new BorderLayout();
		
		setTitle("Vehicles");
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);

		setLayout(layout);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		
		buttonPanel.add(getAddButton());
		
		buttonPanel.add(getEditButton());
		
		buttonPanel.add(getDeleteButton());
		
		buttonPanel.add(getOKButton());
		
		add(buttonPanel, BorderLayout.LINE_START);
		
		add(getJScrollPane0(), BorderLayout.CENTER);
		
		vehicleDetailDialog.setLocationRelativeTo(this);
		
		setSize(600, 400);
		setModal(true);
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getVehicleList());
		}
		return jScrollPane0;
	}

	private JList getVehicleList() {
		if (vehicleList == null) {
			vehicleList = new JList();
			vehicleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			vehicleList.addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent arg0) {
					if (vehicleList.isSelectionEmpty()) {
						deleteButton.setEnabled(false);
						editButton.setEnabled(false);
					} else {
						addButton.setEnabled(true);
						deleteButton.setEnabled(true);
						editButton.setEnabled(true);
					}
					
				}
				
			});
			
			vehicleList.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent m) {
					if (m.getClickCount() == 2) {
						Vehicle  v = (Vehicle) vehicleList.getSelectedValue();
						vehicleDetailDialog.loadData(v);
						vehicleDetailDialog.setVisible(true);
						reloadData();
					}
				}
				
			});
		}
		return vehicleList;
	}

	private JButton getOKButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent event) {
					okButtonCallback(event);
				}
			});
		}
		return okButton;
	}

	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("Delete");
			deleteButton.setEnabled(false);
			deleteButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					Vehicle v = (Vehicle) vehicleList.getSelectedValue();
					String message="Are you sure you want to delete " + v.toString() + "?";
					Object[] options = {"Yes", "No"};
					int answer = JOptionPane.showOptionDialog(vehicleList, message, "Confirm Delete",
							JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options,
							options[1]);
					
					/* No is 1, yes is 0 */
					if (answer == 0) {
						DataManager.getInstance().deleteVehicle(v);
						reloadData();
					}
					
				}
				
			});
		}
		return deleteButton;
	}

	private JButton getEditButton() {
		if (editButton == null) {
			editButton = new JButton();
			editButton.setText("Edit");
			editButton.setEnabled(false);
			editButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Vehicle  v = (Vehicle) vehicleList.getSelectedValue();
					vehicleDetailDialog.loadData(v);
					vehicleDetailDialog.setVisible(true);
					reloadData();
				}
			});
		}
		return editButton;
	}

	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setText("Add");
			addButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					vehicleDetailDialog.loadData(null);
					vehicleDetailDialog.setVisible(true);
					reloadData();
				}
			});
		}
		return addButton;
	}


	private void okButtonCallback(ActionEvent event) {
		setVisible(false);
	}
	
	public void reloadData() {

		
		LinkedList<Vehicle> vehicles = (LinkedList<Vehicle>) DataManager.getInstance().getAllVehicles();
		vehicleList.setListData(vehicles.toArray());

	}



}
